package newfish.contacts.core.resultType;

import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

public class JsonResult extends StrutsResultSupport {

	private String jsonString;

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		String _jsonString = (String)invocation.getStack().findValue(conditionalParse(jsonString, invocation));
		if(_jsonString != null)jsonString = _jsonString;
		HttpServletResponse response = (HttpServletResponse)invocation.getInvocationContext().get(HTTP_RESPONSE);
		response.setContentType("text/javascript");
		response.setContentLength(jsonString.length());
		PrintWriter pw = new PrintWriter(response.getOutputStream());
		pw.print(jsonString);
		pw.flush();
	}
	
}
